package com.jinxiangyang.service.impl;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinxiangyang.domain.Teacher;
import com.jinxiangyang.domain.vo.TeacherQueryVo;
import com.jinxiangyang.mapper.TeacherMapper;
import com.jinxiangyang.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinxiangyang.utils.DateUtils;
import com.jinxiangyang.utils.StringUtils;
import com.jinxiangyang.utils.oos.OOSUtils;
import com.jinxiangyang.utils.result.AjaxResult;
import com.jinxiangyang.utils.seq.UUID;
import net.sf.cglib.core.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jinxiangyang
 * @since 2023-08-12
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    TeacherMapper teacherMapper ;

    @Override
    public AjaxResult findTeacherByPage(long currentPage, long pageSize) {
        Page<Teacher> page = new Page<>(currentPage,pageSize);
        return AjaxResult.success(teacherMapper.selectPage(page,null));
    }

    @Override
    public AjaxResult findTeacherByQuery(long currentPage, long pageSize, TeacherQueryVo teacherQueryVo) {
        Page<Teacher> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        String name = teacherQueryVo.getName();
        String professional = teacherQueryVo.getProfessional();
        String joinDateBegin = teacherQueryVo.getJoinDateBegin();
        String joinDateEnd = teacherQueryVo.getJoinDateEnd();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("rel_name",name);
        }
        if (!StringUtils.isEmpty(professional)) {
            queryWrapper.eq("professional",professional);
        }
        if (!StringUtils.isEmpty(joinDateBegin)) {
            queryWrapper.ge("join_date",joinDateBegin);
        }
        if (!StringUtils.isEmpty(joinDateEnd)){
            queryWrapper.le("join_date",joinDateEnd);
        }
        return AjaxResult.success(teacherMapper.selectPage(page, queryWrapper));
    }

    @Override
    public boolean addTeacher(Teacher teacher) {
        Date now = DateUtils.getNowDate();
        teacher.setCreateTime(now);
        teacher.setUpdateTime(now);
        teacher.setIsDelete(1);
        teacher.setUid(UUID.fastUUID().getMostSignificantBits());
        int flag = teacherMapper.insert(teacher);
        if (flag == 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public String avatarUpload(MultipartFile file) {
        return OOSUtils.avatarUpload(file);
    }


}
